#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

typedef struct qNode
{
    int data;
    struct qNode* next;
}qNode;

typedef struct {
    qNode* head;
    qNode* tail;
    int size;
    int capacity;
} MyCircularQueue;

bool myCircularQueueIsEmpty(MyCircularQueue* obj);
bool myCircularQueueIsFull(MyCircularQueue* obj);

MyCircularQueue* myCircularQueueCreate(int k) {
    MyCircularQueue* cq = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    cq->head = cq->tail = NULL;
    cq->capacity = k;
    cq->size = 0;

    return cq;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if (myCircularQueueIsFull(obj))
    {
        return false;
    }
    qNode* newNode = (qNode*)malloc(sizeof(qNode));
    newNode->data = value;
    newNode->next = NULL;

    if (obj->size == 0)
    {
        obj->head = obj->tail = newNode;
    }
    else
    {
        obj->tail->next = newNode;
        newNode->next = obj->head;

        obj->tail = newNode;
    }
    obj->size++;

    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
    {
        return false;
    }
    qNode* next = obj->head->next;
    free(obj->head);
    obj->head = next;
    obj->size--;
    return true;
}

int myCircularQueueFront(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
        return -1;
    return obj->head->data;
}

int myCircularQueueRear(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
        return -1;
    return obj->tail->data;
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return !obj->size;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
    return obj->size == obj->capacity;
}

void myCircularQueueFree(MyCircularQueue* obj) {
    if (myCircularQueueIsEmpty(obj))
    {
        free(obj);
        return;
    }

    qNode* cur = obj->head;
    while (cur->next != obj->head)
    {
        qNode* next = cur->next;
        free(cur);
        cur = next;
    }
    free(obj);
}


int main()
{
    int k = 4;
    int value = 0;
    MyCircularQueue* obj = myCircularQueueCreate(k);
    bool param_1 = myCircularQueueEnQueue(obj, value);

    bool param_2 = myCircularQueueDeQueue(obj);

    int param_3 = myCircularQueueFront(obj);

    int param_4 = myCircularQueueRear(obj);

    bool param_5 = myCircularQueueIsEmpty(obj);

    bool param_6 = myCircularQueueIsFull(obj);

    myCircularQueueFree(obj);
    return 0;
}
